/*
 * @Author: your name
 * @Date: 2021-12-23 09:20:01
 * @LastEditTime: 2022-03-07 12:12:56
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \vue\vue-douban-rd\routes\index.js
 */
const db = require('./db')
const router = require('koa-router')()

router.get("/getList", async (ctx)=>{
  let sql = "SELECT * FROM t_tv WHERE tv_id<=8";
  let List = await db.query(sql,[]);
  ctx.body = List
})
router.get("/getMoviesHomeList", async (ctx)=>{
  let sql = "SELECT * FROM t_tvs WHERE tvs_id<=8";
  let List = await db.query(sql,[]);
  ctx.body = List
})
router.get("/getBooksHomeList", async (ctx)=>{
  let sql = "SELECT * FROM t_books WHERE book_id<=8";
  let List = await db.query(sql,[]);
  ctx.body = List
})
router.get("/getMovieList", async (ctx)=>{
  let sql = "SELECT * FROM t_tv";
  let List = await db.query(sql,[]);
  ctx.body = List
})
router.get("/getMoviesList", async (ctx)=>{
  let sql = "SELECT * FROM t_tvs";
  let List = await db.query(sql,[]);
  ctx.body = List
})
router.get("/getBooksList", async (ctx)=>{
  let sql = "SELECT * FROM t_books";
  let List = await db.query(sql,[]);
  ctx.body = List
})
router.get("/getMovie", async (ctx)=>{
  let movieId = ctx.query.id;
  let sql = "SELECT * FROM t_tv WHERE tv_id=?";
  let List = await db.query(sql,[movieId]);
  ctx.body = List
})
router.post("/postBarrage", async (ctx)=>{
  let {userId,barrage,movieId,userName} = ctx.request.body;
  let sql = "INSERT INTO t_barrage(user_id,barrage,tv_id,username) VALUES(?,?,?,?)";
  let List = await db.query(sql,[userId,barrage,movieId,userName]);
  if(List.affectedRows > 0){
    ctx.body = {
      code : 2200,
      data:{
        state: "success",
      }
    }
  }else{
    ctx.body = {
      code : 2500,
      data:{
        state: "fail",
      }
    }
  }
})
router.post("/postBarrages", async (ctx)=>{
  let {userId,userName,barrage,movieId} = ctx.request.body;
  let sql = "INSERT INTO t_barrage(user_id,barrage,tvs_id,username) VALUES(?,?,?,?)";
  let List = await db.query(sql,[userId,barrage,movieId,userName]);
  if(List.affectedRows > 0){
    ctx.body = {
      code : 2200,
      data:{
        state: "success",
      }
    }
  }else{
    ctx.body = {
      code : 2500,
      data:{
        state: "fail",
      }
    }
  }
})
router.get("/getComment", async (ctx)=>{
  let movieId = ctx.query.id;
  let sql = "SELECT * FROM t_barrage WHERE tv_id=?";
  let List = await db.query(sql,[movieId]);
  ctx.body = List
})
router.get("/getComments", async (ctx)=>{
  let movieId = ctx.query.id;
  let sql = "SELECT * FROM t_barrage WHERE tvs_id=?";
  let List = await db.query(sql,[movieId]);
  ctx.body = List
})
router.get("/addPlay", async (ctx)=>{
  let movieId = ctx.query.id;
  let sql = "UPDATE t_tv SET tv_play=tv_play+1 WHERE tv_id=?";
  let List = await db.query(sql,[movieId]);
  ctx.body = List
})
router.get("/addsPlay", async (ctx)=>{
  let movieId = ctx.query.id;
  let sql = "UPDATE t_tvs SET tvs_play=tvs_play+1 WHERE tvs_id=?";
  let List = await db.query(sql,[movieId]);
  ctx.body = List
})
router.get("/getMovies", async (ctx)=>{
  let movieId = ctx.query.id;
  let sql = "SELECT * FROM t_tvs WHERE tvs_id=?";
  let List = await db.query(sql,[movieId]);
  ctx.body = List
})
router.get("/getContent", async (ctx)=>{
  let movieId = ctx.query.id;
  let sql = "SELECT * FROM t_content WHERE tvs_id=?";
  let List = await db.query(sql,[movieId]);
  ctx.body = List
})
router.get("/getInformation", async (ctx)=>{
  let userId = ctx.query.id
  let sql = "SELECT * FROM t_user WHERE user_id=?";
  let List = await db.query(sql,[userId]);
  ctx.body = List
})
router.get("/getAllBarrage", async (ctx)=>{
  let sql = "SELECT barrage,user_id,bag_id,username FROM t_barrage";
  let List = await db.query(sql,[]);
  
  ctx.body = List
  
})
router.get("/getBarrage", async (ctx)=>{
  let userId = ctx.query.id
  let sql = "SELECT barrage,bag_id FROM t_barrage WHERE user_id=?";
  let List = await db.query(sql,[userId]);
  ctx.body = List
})
router.get("/getUser", async (ctx)=>{
  let sql = "SELECT * FROM t_user ";
  let List = await db.query(sql,[]);
  ctx.body = List
})
router.get("/deleteBarrage", async (ctx)=>{
  let bagId = ctx.query.id;
  let sql = "DELETE FROM t_barrage WHERE bag_id=?";
  await db.query(sql,[bagId])
})
router.get("/deleteMovie", async (ctx)=>{
  let tvId = ctx.query.id;
  let sql = "DELETE FROM t_tv WHERE tv_id=?";
  await db.query(sql,[tvId])
})
router.post("/submitComment", async (ctx)=>{
  let {userId,comment,movieId,userName} = ctx.request.body;
  let sql = "INSERT INTO t_comment(user_id,comment,tv_id,username) VALUES(?,?,?,?)";
  let List = await db.query(sql,[userId,comment,movieId,userName]);
  if(List.affectedRows > 0){
    ctx.body = {
      code : 2200,
      data:{
        state: "success",
      }
    }
  }else{
    ctx.body = {
      code : 2500,
      data:{
        state: "fail",
      }
    }
  } 
})
router.post("/submitComments", async (ctx)=>{
  let {userId,comment,movieId,userName} = ctx.request.body;
  let sql = "INSERT INTO t_comment(user_id,comment,tvs_id,username) VALUES(?,?,?,?)";
  let List = await db.query(sql,[userId,comment,movieId,userName]);
  if(List.affectedRows > 0){
    ctx.body = {
      code : 2200,
      data:{
        state: "success",
      }
    }
  }else{
    ctx.body = {
      code : 2500,
      data:{
        state: "fail",
      }
    }
  } 
})
router.get("/getSubmit", async (ctx)=>{
  let movieId = ctx.query.id;
  let sql = "SELECT * FROM t_comment WHERE tv_id=?";
  let List = await db.query(sql,[movieId]);
  ctx.body = List
})
router.get("/getSubmits", async (ctx)=>{
  let movieId = ctx.query.id;
  let sql = "SELECT * FROM t_comment WHERE tvs_id=?";
  let List = await db.query(sql,[movieId]);
  ctx.body = List
})
router.get("/getOwnComment", async (ctx)=>{
  let userId = ctx.query.id
  let sql = "SELECT comment,comm_id FROM t_comment WHERE user_id=?";
  let List = await db.query(sql,[userId]);
  ctx.body = List
})
router.get("/getAllComment", async (ctx)=>{
  let sql = "SELECT comment,comm_id,user_id,username FROM t_comment ";
  let List = await db.query(sql,[]);
  ctx.body = List
})
router.get("/deleteComment", async (ctx)=>{
  let commId = ctx.query.id;
  let sql = "DELETE FROM t_comment WHERE comm_id=?";
  await db.query(sql,[commId])
})
router.get("/deleteOwnComment", async (ctx)=>{
  let commId = ctx.query.id;
  let sql = "DELETE FROM t_comment WHERE comm_id=?";
  await db.query(sql,[commId])
})
router.get("/deleteOwnBarrage", async (ctx)=>{
  let bagId = ctx.query.id;
  let sql = "DELETE FROM t_barrage WHERE bag_id=?";
  await db.query(sql,[bagId])
})
module.exports = router
